const fs = require('fs');

class CsvWriter {
  constructor(path) {
    this._stream = fs.createWriteStream(path);
  }

  writeUtf8Bom() {
    this._stream.write('\uFEFF');
  }

  writeRow(...cols) {
    let appendDot = false;
    for (const col of cols) {
      if (appendDot) {
        this._stream.write(',');
      }
      appendDot = true;
      this._stream.write('"');
      this._stream.write(('' + col).replace(/"/g, '""'));
      this._stream.write('"');
    }
    this._stream.write('\n');
  }

  end() {
    this._stream.end();
  }
}

module.exports = CsvWriter;
